import { _decorator, Animation, AudioClip, Collider2D, Component, Contact2DType, IPhysics2DContact, Sprite } from 'cc';
import { Bullet1 } from './Bullet1';
import { GameManager } from './GameManager';
import { EnemyManager } from './EnemyManager';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Component {

    @property
    speed: number = 300;

    @property(Animation)
    anim: Animation = null;

    @property
    hp: number = 1;

    collider: Collider2D = null;

    @property
    animHit: string = "";
    @property
    animDown: string = "";

    @property
    score: number = 100;
    @property(AudioClip)
    enemyDownAudio:AudioClip=null;




    start() {
        // 注册单个碰撞体的回调函数
        this.collider = this.getComponent(Collider2D);
        if (this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }

    update(deltaTime: number) {
        const p = this.node.position;
        if (this.hp > 0) {
            this.node.setPosition(p.x, p.y - this.speed * deltaTime, p.z);
        }
        if (this.node.position.y < -852) {
            this.node.destroy();
            EnemyManager.getInstance().removeEnemy(this.node);
        }
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        console.log("onBeginContact")
        if (otherCollider.getComponent(Bullet1)) {
            otherCollider.enabled = false;
            otherCollider.getComponent(Sprite).enabled = false;
        }

        this.hp -= 1;
        if (this.hp > 0) {
            this.anim.play(this.animHit);
        } else {
            this.anim.play(this.animDown);
        }


        if (this.hp <= 0) {
            this.dead();

        }
    }

    protected onDestroy(): void {
        if (this.collider) {
            this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }

    haveDead: boolean = false;

    dead() {
        if (this.haveDead) return;
        AudioMgr.inst.playOneShot(this.enemyDownAudio);
        EnemyManager.getInstance().removeEnemy(this.node);
        GameManager.getInstance().addScore(this.score);
        if (this.collider) {
            this.collider.enabled = false;
        }
        this.scheduleOnce(function () {
            this.node.destroy();
        }, 1);
    }

    killNow() {
        if (this.hp <= 0) {
            return;
        }
        this.hp = 0;
        this.anim.play(this.animDown);
        this.dead();
        
    }
}


